﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using ScreenMonitor.Common;
using ScreenMonitor.ServerLib;

namespace ServerUI
{
    public partial class ScreenViewerMini : UserControl
    {

        #region========构造函数========
        public ScreenViewerMini(ClientObject Client)
        {
            InitializeComponent();
            this.Viewer = new ScreenViewer(this);
            this.Client = Client;
            Client.ImageChanged += Client_ImageChanged;
            Client.InfoStrChanged += Client_InfoStrChanged;
            Client.StateChanged += Client_StateChanged;
            Client.SignStateChanged += Client_SignStateChanged;
        }
        #endregion

        #region========私有函数========

        void Client_SignStateChanged(object sender, EventArgs e)
        {
            if (this.lbIsSigned.InvokeRequired)
            {
                EventHandler del = new EventHandler(Client_SignStateChanged);
                this.lbIsSigned.BeginInvoke(del, sender, e);
            }
            else
            {
                switch (Client.SignState)
                {
                    case SignState.Signed:
                        this.lbIsSigned.Text = "已签到";
                        this.lbIsSigned.BackColor = Color.AliceBlue;
                        break;
                    case SignState.Signing:
                        this.lbIsSigned.Text = "正在签到";
                        this.lbIsSigned.BackColor = Color.GreenYellow;
                        break;
                    case SignState.UnSigned:
                        this.lbIsSigned.Text = "签到中断";
                        this.lbIsSigned.BackColor = Color.DarkRed;
                        break;
                    case SignState.NeverSigned:
                        this.lbIsSigned.Text = "从未签到";
                        this.lbIsSigned.BackColor = Color.Gray;
                        break;
                }
            }
        }

        void Client_StateChanged(object sender, EventArgs e)
        {
            if (this.InfoTxt.InvokeRequired)
            {
                EventHandler del = new EventHandler(Client_StateChanged);
                this.InfoTxt.BeginInvoke(del, sender, e);
            }
            else
            {
                switch (Client.State)
                {
                    case ClientState.Connected:
                        this.InfoTxt.BackColor = Color.DeepSkyBlue;
                        break;
                    case ClientState.Init:
                        this.InfoTxt.BackColor = Color.GreenYellow;
                        break;
                    case ClientState.DisConnected:
                        this.InfoTxt.BackColor = Color.OrangeRed;
                        break;
                }
            }
        }

        void Client_InfoStrChanged(object sender, EventArgs e)
        {
            if (this.InfoTxt.InvokeRequired)
            {
                EventHandler del = new EventHandler(Client_InfoStrChanged);
                this.InfoTxt.BeginInvoke(del, sender, e);
            }
            else
            {
                InfoTxt.Text = Client.InfoStr;
                if(IsViewerOpened)
                    Viewer.InfoTxt = Client.InfoStr;
            }
        }

        void Client_ImageChanged(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler del = new EventHandler(Client_ImageChanged);
                this.BeginInvoke(del, sender, e);
            }
            else
            {
                this.preViewImg.Image = Client.ScreenImage;
                if(IsViewerOpened)
                    this.Viewer.Img = Client.ScreenImage;
                else
                    this.Size = new Size(Client.ScreenImage.Width, Client.ScreenImage.Height + this.InfoTxt.Height + this.tableLayoutPanel1.Height);
            }
        }

        private void 打开新窗口监控ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Viewer.Show(Program.MainWnd.MainDockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document);
            IsViewerOpened = true;
        }

        #endregion

        #region==========属性==========

        /// <summary>
        /// 获取与控件关联的客户端对象
        /// </summary>
        public ClientObject Client { get; private set; }
        /// <summary>
        /// 获取与控件关联的屏幕浏览窗体
        /// </summary>
        public ScreenViewer Viewer { get; private set; }
        /// <summary>
        /// 表示大窗口是否已经打开
        /// </summary>
        public bool IsViewerOpened { get;internal set; }

        #endregion

    }
}
